﻿namespace VarianceReportWinUI
{
    using System;
    using System.Data.SqlTypes;
    using System.Runtime.CompilerServices;
    using System.Text.RegularExpressions;

    public static class Extensions
    {
        private static readonly Regex mRegexGuid = new Regex(@"^((\A\([a-f\d]{8}(-[a-f\d]{4}){3}-[a-f\d]{12}\)\z)|(\A\{[a-f\d]{8}(-[a-f\d]{4}){3}-[a-f\d]{12}\}\z)|(\A\{\s*0x[a-f\d]{8}\s*,\s*(0x[a-f\d]{4}\s*,\s*){2}\{\s*(0x[a-f\d]{2}\s*,\s*){7}0x[a-f\d]{2}\s*\}\s*}\z)|(\A[a-f\d]{32}\z))$", RegexOptions.IgnorePatternWhitespace | RegexOptions.Compiled | RegexOptions.IgnoreCase);

        public static bool BoolValue(this object pValue)
        {
            return pValue.StringValueOrEmpty().BoolValue();
        }

        public static bool BoolValue(this string pValue)
        {
            if (((pValue != null) && (pValue != string.Empty)) && (pValue.Trim().Length != 0))
            {
                bool result = false;
                if (bool.TryParse(pValue, out result))
                {
                    return result;
                }
                if (((((pValue.ToUpper() == "Y") || (pValue.ToUpper() == "YES")) || ((pValue.ToUpper() == "1") || (pValue.ToUpper() == "-1"))) || (pValue.ToUpper() == "T")) || (pValue.ToUpper() == "TRUE"))
                {
                    return true;
                }
                if ((((pValue.ToUpper() != "N") && (pValue.ToUpper() != "NO")) && ((pValue.ToUpper() != "0") && (pValue.ToUpper() != "F"))) && (pValue.ToUpper() != "FALSE"))
                {
                    throw new Exception("Invalid value for boolean type: " + pValue + ".");
                }
            }
            return false;
        }

        public static bool? BoolValueOrNull(this string pValue)
        {
            bool result = false;
            if (bool.TryParse(pValue, out result))
            {
                return new bool?(result);
            }
            if ((((pValue.ToUpper() == "Y") || (pValue.ToUpper() == "YES")) || ((pValue.ToUpper() == "1") || (pValue.ToUpper() == "T"))) || (pValue.ToUpper() == "TRUE"))
            {
                return true;
            }
            if ((((pValue.ToUpper() == "N") || (pValue.ToUpper() == "NO")) || ((pValue.ToUpper() == "0") || (pValue.ToUpper() == "F"))) || (pValue.ToUpper() == "FALSE"))
            {
                return false;
            }
            return null;
        }

        public static DateTime DateValueOrMin(this string pValue)
        {
            DateTime time;
            if (DateTime.TryParse(pValue, out time))
            {
                return time;
            }
            return DateTime.MinValue;
        }

        public static DateTime? DateValueOrNull(this object pValue)
        {
            DateTime time;
            if ((pValue != null) && DateTime.TryParse(pValue.ToString(), out time))
            {
                return new DateTime?(time);
            }
            return null;
        }

        public static DateTime? DateValueOrNull(this string pValue)
        {
            DateTime time;
            if (DateTime.TryParse(pValue, out time))
            {
                return new DateTime?(time);
            }
            return null;
        }

        public static string DBString(this bool pValue)
        {
            if (pValue)
            {
                return " '1' ";
            }
            return "'0'";
        }

        public static string DBString(this int pValue)
        {
            return ("'" + pValue.ToString() + "'");
        }

        public static string DBString(this bool? pValue)
        {
            if (!pValue.HasValue)
            {
                return " NULL ";
            }
            return pValue.Value.DBString();
        }

        public static string DBString(this DateTime? pValue)
        {
            if (!pValue.HasValue)
            {
                return " NULL ";
            }
            return pValue.Value.DBString();
        }

        public static string DBString(this double? pValue)
        {
            if (!pValue.HasValue)
            {
                return " NULL ";
            }
            return pValue.Value.DBString();
        }

        public static string DBString(this int? pValue)
        {
            if (!pValue.HasValue)
            {
                return " NULL ";
            }
            return pValue.Value.DBString();
        }

        public static string DBString(this DateTime pValue)
        {
            try
            {
                return ("'" + SqlDateTime.Parse(pValue.ToString()).ToString() + "'");
            }
            catch
            {
                return " NULL ";
            }
        }

        public static string DBString(this double pValue)
        {
            return ("'" + pValue.ToString() + "'");
        }

        public static string DBString(this string pValue)
        {
            if (pValue.IsEmpty())
            {
                return " NULL ";
            }
            return ("'" + pValue.Replace("'", "''") + "'");
        }

        public static string DBString(this string pValue, int pMaxLength)
        {
            if (pValue.IsEmpty())
            {
                return " NULL ";
            }
            if (pValue.Length > pMaxLength)
            {
                return ("'" + pValue.Substring(0, pMaxLength).Replace("'", "''") + "'");
            }
            return ("'" + pValue.Replace("'", "''") + "'");
        }

        public static decimal? DecimalValueOrNull(this string pValue)
        {
            decimal num;
            if (decimal.TryParse(pValue, out num))
            {
                return new decimal?(num);
            }
            return null;
        }

        public static decimal DecimalValueOrZero(this string pValue)
        {
            decimal num;
            if (decimal.TryParse(pValue, out num))
            {
                return num;
            }
            return 0M;
        }

        public static double? DoubleValueOrNull(this string pValue)
        {
            double num;
            if (double.TryParse(pValue, out num))
            {
                return new double?(num);
            }
            return null;
        }

        public static double? DoubleValueOrNull(this string pValue, int decimals)
        {
            double? nullable = pValue.DoubleValueOrNull();
            if (nullable.HasValue)
            {
                nullable = new double?(Math.Round(nullable.Value, decimals));
            }
            return nullable;
        }

        public static double DoubleValueOrZero(this string pValue)
        {
            double num;
            if (double.TryParse(pValue, out num))
            {
                return num;
            }
            return 0.0;
        }

        public static string GetValueOrDefault(this object pValue, string pDefault)
        {
            if (pValue == null)
            {
                return pDefault;
            }
            return pValue.ToString();
        }

        public static bool HasValue(this object pValue)
        {
            return !pValue.IsEmpty();
        }

        public static int IntValueOrDefault(this string pValue, int pDefaultValue)
        {
            int num;
            if (int.TryParse(pValue, out num))
            {
                return num;
            }
            return pDefaultValue;
        }

        public static int? IntValueOrNull(this string pValue)
        {
            if (pValue.IsNumeric())
            {
                return new int?(Convert.ToInt32(pValue.DecimalValueOrNull()));
            }
            return null;
        }

        public static int IntValueOrZero(this object pValue)
        {
            if (pValue.StringValueOrEmpty().IsNumeric())
            {
                return Convert.ToInt32(pValue.ToString().DecimalValueOrZero());
            }
            return 0;
        }

        public static int IntValueOrZero(this string pValue)
        {
            if (pValue.IsNumeric())
            {
                return Convert.ToInt32(pValue.DecimalValueOrZero());
            }
            return 0;
        }

        public static bool IsEmpty(this object pValue)
        {
            return ((((pValue == null) || pValue.Equals(string.Empty)) || DBNull.Value.Equals(pValue)) || (pValue.ToString().Trim().Length == 0));
        }

        public static bool IsGuid(this string g)
        {
            return (!string.IsNullOrEmpty(g) || mRegexGuid.IsMatch(g));
        }

        public static bool IsNumeric(this string pValue)
        {
            double result = 0.0;
            return double.TryParse(pValue, out result);
        }

        public static string Left(this string pValue, int pMaxLength)
        {
            if (pValue == null)
            {
                return "";
            }
            if (pValue.Length > pMaxLength)
            {
                pValue = pValue.Substring(0, pMaxLength);
            }
            return pValue;
        }

        public static string LeftOrNull(this string pValue, int pMaxLength)
        {
            if (pValue == null)
            {
                return null;
            }
            if (pValue.Length > pMaxLength)
            {
                pValue = pValue.Substring(0, pMaxLength);
            }
            return pValue;
        }

        public static long? LongValueOrNull(this string pValue)
        {
            long num;
            if (long.TryParse(pValue, out num))
            {
                return new long?(num);
            }
            return null;
        }

        public static long LongValueOrZero(this string pValue)
        {
            long num;
            if (long.TryParse(pValue, out num))
            {
                return num;
            }
            return 0L;
        }

        public static string Right(this string pValue, int pMaxLength)
        {
            if (pValue == null)
            {
                return "";
            }
            if (pValue.Length > pMaxLength)
            {
                pValue = pValue.Substring(pValue.Length - pMaxLength);
            }
            return pValue;
        }

        public static string RightOrNull(this string pValue, int pMaxLength)
        {
            if (pValue == null)
            {
                return null;
            }
            if (pValue.Length > pMaxLength)
            {
                pValue = pValue.Substring(pValue.Length - pMaxLength);
            }
            return pValue;
        }

        public static short? ShortValueOrNull(this string pValue)
        {
            short num;
            if (short.TryParse(pValue, out num))
            {
                return new short?(num);
            }
            return null;
        }

        public static short ShortValueOrZero(this string pValue)
        {
            short num;
            if (short.TryParse(pValue, out num))
            {
                return num;
            }
            return 0;
        }

        public static string SQLDateTimeDBValue(this string pValue)
        {
            string str = " NULL ";
            if (!pValue.IsEmpty())
            {
                object obj2 = pValue.SqlDateTimeSringOrDBNull();
                if (obj2 != DBNull.Value)
                {
                    str = "'" + obj2.ToString() + "'";
                }
            }
            return str;
        }

        public static DateTime? SqlDateTimeOrNull(this object pValue)
        {
            DateTime? nullable = null;
            if ((pValue is DateTime) || (pValue is DateTime?))
            {
                if (pValue != null)
                {
                    nullable = new DateTime?((DateTime) pValue);
                }
            }
            else if (!pValue.IsEmpty())
            {
                nullable = pValue.ToString().DateValueOrNull();
            }
            if (nullable.HasValue && ((nullable.Value < DateTime.Parse("1/1/1753 12:00:00 AM")) || (nullable.Value > DateTime.Parse("12/31/9999 11:59:59 PM"))))
            {
                nullable = null;
            }
            return nullable;
        }

        public static object SqlDateTimeSringOrDBNull(this string pValue)
        {
            try
            {
                return SqlDateTime.Parse(pValue).ToString();
            }
            catch
            {
                return DBNull.Value;
            }
        }

        public static string StringValueOrEmpty(this DateTime? pValue)
        {
            if (!pValue.HasValue)
            {
                return "";
            }
            return pValue.Value.ToString();
        }

        public static string StringValueOrEmpty(this double? pValue)
        {
            if (!pValue.HasValue)
            {
                return "";
            }
            return pValue.Value.ToString();
        }

        public static string StringValueOrEmpty(this object pValue)
        {
            if (pValue == null)
            {
                return "";
            }
            return pValue.ToString();
        }

        public static uint UIntValueOrDefault(this string pValue, uint pDefaultValue)
        {
            uint num;
            if (uint.TryParse(pValue, out num))
            {
                return num;
            }
            return pDefaultValue;
        }
    }
}

